﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Launchkit_Backend
{
    public partial class DlgCreateParam : Form
    {
        public DlgCreateParam()
        {
            InitializeComponent();
        }

        private void DlgCreateParam_Load(object sender, EventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            TextBox ctl = (TextBox)sender;
            String str = ctl.Text;
            bool enterPressed = false;
            if (str.Contains("\r"))
            {
                str = str.Replace("\r", "");
                enterPressed = true;
            }
            if (str.Contains("\n"))
            {
                str = str.Replace("\n", "");
                enterPressed = true;
            }

            if (enterPressed) {
                ctl.Text = str;
            }

            if (enterPressed && str!="")
            {
                txtParam.Focus();
                
            }
            else
            {
            }

        }

        private void txtParam_TextChanged(object sender, EventArgs e)
        {
            TextBox ctl = (TextBox)sender;
            String str = ctl.Text;
            bool enterPressed = false;
            if (str.Contains("\r"))
            {
                str = str.Replace("\r", "");
                enterPressed = true;
            }
            if (str.Contains("\n"))
            {
                str = str.Replace("\n", "");
                enterPressed = true;
            }

            btnOk.Enabled = str != "" && txtName.Text != "";

            if (enterPressed && btnOk.Enabled)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                ctl.Text = str;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        public static model.EntryData InputParam(Form container, String typehint) {
            DlgCreateParam dlg = new DlgCreateParam();
            dlg.lblParamHint.Text = typehint;
            if (dlg.ShowDialog(container) == DialogResult.OK) {
                model.EntryData ed = new model.EntryData()
                {
                    action = dlg.txtName.Text,
                    param = dlg.txtParam.Text
                };
                dlg.Dispose();
                return ed;
            }
            return null;
        }
    }
}
